<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage DistributorPricelist
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The model for distributor pricelist
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage DistributorPricelist
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_DistributorPricelist extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'distributor_pricelist';

    /**
     * Save distributor pricelist
     * @param array $row
     */
    public function save($row)
    {
        // Check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $id = $this->insert($row);
        }
        return $id;
    }

    /**
     * Get all pricelists
     * @return array
     */
    public function getAll()
    {
        $query = $this->getAdapter()->select();
        $query->from(array('dp' => $this->_name), array('url', 'local_file', 'format', 'header_rows', 'name_column', 'price_column', 'distributor_id'));
        $query->join(array('d' => 'distributor'), 'd.id = dp.distributor_id', '');
        $query->join(array('c' => 'currency'), 'c.id = d.currency_id', 'c.rate as currency_rate');
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get pricelist by filter 
     * @return array 
     */
    public function getAllByFilter($distributorName, $url, $localFile, $format, $headerRows, $nameColumn, $priceColumn, $currencyCode, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('dp' => $this->_name), array('url', 'local_file', 'format', 'header_rows', 'name_column', 'price_column', 'distributor_id'));
        $query->join(array('d' => 'distributor'), 'd.id = dp.distributor_id', '');
        $query->join(array('c' => 'currency'), 'c.id = d.currency_id', 'c.code as currency_code');

        // Check filters
        if ($distributorName) {
            $query->where('d.name LIKE ?', $distributorName . '%');
        }
        if ($url) {
            $query->where('dp.url LIKE ?', '%' . $url . '%');
        }
        if ($localFile) {
            $query->where('dp.local_file LIKE ?', $localFile . '%');
        }
        if ($format) {
            $query->where('dp.format LIKE ?', $format . '%');
        }
        if ($headerRows) {
            $query->where('dp.header_rows = ?', $headerRows);
        }
        if ($nameColumn) {
            $query->where('dp.name_column = ?', $nameColumn);
        }
        if ($priceColumn) {
            $query->where('dp.price_column = ?', $priceColumn);
        }
        if ($currencyCode) {
            $query->where('c.code LIKE ?', $currencyCode . '%');
        }

        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }
        $query->limitPage($page, $rowCount);
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of distributor by filter 
     * @return integer 
     */
    public function getCountByFilter($distributorName, $url, $localFile, $format, $headerRows, $nameColumn, $priceColumn, $currencyCode, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('dp' => $this->_name), new Zend_Db_Expr('COUNT(*)'));
        $query->join(array('d' => 'distributor'), 'd.id = dp.distributor_id', '');
        $query->join(array('c' => 'currency'), 'c.id = d.currency_id', '');

        // Check filters
        if ($distributorName) {
            $query->where('d.name LIKE ?', $distributorName . '%');
        }
        if ($url) {
            $query->where('dp.url LIKE ?', '%' . $url . '%');
        }
        if ($localFile) {
            $query->where('dp.local_file LIKE ?', $localFile . '%');
        }
        if ($format) {
            $query->where('dp.format LIKE ?', $format . '%');
        }
        if ($headerRows) {
            $query->where('dp.header_rows = ?', $headerRows);
        }
        if ($nameColumn) {
            $query->where('dp.name_column = ?', $nameColumn);
        }
        if ($priceColumn) {
            $query->where('dp.price_column = ?', $priceColumn);
        }
        if ($currencyCode) {
            $query->where('c.code LIKE ?', $currencyCode . '%');
        }
        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Get distributor pricelist by ID
     * @param integer $id
     * @return array
     */
    public function getById($id)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id', 'url', 'local_file', 'format', 'header_rows', 'name_column', 'price_column', 'distributor_id'));
        $query->where('id = ?', $id);
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
